const Game = require("./Game");

cc.Class({
    extends: cc.Component,

    properties: {
        chipPrefab: cc.Prefab,
        btnChips: {
            type: cc.Node,
            default: [],
        },
        chipValues: {
            type: cc.Integer,
            default: [],
        },
        // 投掷点
        anchorChipToss: cc.Node,
    },

    init: function () {
        // 注册下注按钮等
        cc.log('init bet.js');
        this._registerBtns();
    },

    // 注册按钮
    _registerBtns: function () {
        const self = this;
        const registerBtn = function (index) {
            self.btnChips[index].on(cc.Node.EventType.TOUCH_START, function (event) {
                const isAdd = Game.instance.addStake(self.chipValues[index]);
                if(isAdd) {
                    cc.log('playing add chip');
                    self.playAddChip();
                }
            }, this)
        };

        for(let i = 0; i < this.btnChips.length; ++i) {
            registerBtn(i);
        }
    },

    playAddChip: function () {
        // 半径为 1
        const startPos = cc.v2((Math.random() - 0.5) * 2 * 50, (Math.random() - 0.5) * 2 * 50)
        const chip = cc.instantiate(this.chipPrefab);
        this.anchorChipToss.addChild(chip);
        chip.setPosition(startPos);
        chip.getComponent("TossChip").play();
    },

    resetChips: function () {
        Game.instance.resetStake();
        Game.instance.info.enabled = false;
        this.resetTossedChips();
    },

    resetTossedChips: function () {
        // need node or not ?
        this.anchorChipToss.removeAllChildren();
    }

});
